﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //while (true)
            //{
            //    CreateSymbol();
            //}

            //return;

            //CreateOperation();

            //CrearMovimiento();

            Bolseye.Support.ParseTextFiles2();

            return;


             var m = new Bolseye.ParserMaster();


            //Bolseye.Parsing.ParseSimbolos();
        }



        public static void CrearMovimiento()
        {
            Console.WriteLine("CREATE OPERATION");

            var db = Bolseye.Support.GetDB();

            int simbolo = VerificarSimbolo(db);

            float capital = GetCurrency("Capital: ");

            Console.WriteLine("Description: ");
            string descripcion = Console.ReadLine();

            var move = new Bolseye.Movimiento();
            move.Capital = capital;
            move.Descripcion = descripcion;
            move.FechaCreacion = DateTime.UtcNow;
            move.Simbolo = simbolo;

            db.AddToMovimientos(move);

            int aff = db.SaveChanges();

            Console.WriteLine("MOVIMIENTO CREADO EXITOSAMENTE ! Presionamela para continuar.");
            Console.ReadKey();
        }



        public static void CreateOperation()
        {
            Console.WriteLine("CREATE OPERATION");

            var db = Bolseye.Support.GetDB();
            
            int simbolo = VerificarSimbolo(db);

            float pcioCompra = 0;
            float pcioVenta = 0;

            while (true)
            {
                pcioCompra = GetCurrency("Precio de Compra: ");
                pcioVenta = GetCurrency("Precio de Venta: ");

                if (pcioCompra >= pcioVenta)
                {
                    Console.WriteLine("El precio de venta debe ser mayor que el de compra.");
                    continue;
                }
                break;
            }

            var cantidad = GetCurrency("Cantidad: ");

            var compra = cantidad * pcioVenta;

            Console.WriteLine("Compra estimada: {0}",
                Bolseye.Support.ValueToString(compra)
                );

            var pcioLim = Bolseye.Support.CalculateBuyingCost(true, compra);
            var pcioMdo = Bolseye.Support.CalculateBuyingCost(false, compra);

            Console.WriteLine(
                "Costo ingreso estimado • Precio limite {0} • Precio mercado {1}",
                Bolseye.Support.ValueToString(pcioLim),
                Bolseye.Support.ValueToString(pcioMdo)
                );

            var costoIngreso = GetCurrency("Costo Ingreso: ");

            var capitalNeto = compra - costoIngreso;

            Console.WriteLine(
                "Capital Neto estimado {0}",
                Bolseye.Support.ValueToString(capitalNeto)
                );

            var capital = GetCurrency("Capital Neto: ");


            var op = new Bolseye.Operacion();
            op.Cantidad = cantidad;
            op.Capital = capitalNeto;
            op.CostoIngreso = costoIngreso;
            op.FechaCreacion = DateTime.UtcNow;
            op.PcioCompra = pcioCompra;
            op.PcioVenta = pcioVenta;
            op.Simbolo = simbolo;

            db.AddToOperaciones(op);

            int aff = db.SaveChanges();

            Console.WriteLine("OPERACION AGREGADA EXITOSAMENTE ! Presionamela para continuar.");
            Console.ReadKey();
        }



        private static int VerificarSimbolo(Bolseye.BolseyeEnt db)
        {
            while (true)
            {
                Console.Write("Simbolo: ");
                var symbol = (Console.ReadLine() + "").Trim().ToUpper();

                var s = from simb in db.Simbolos
                        where simb.Simb == symbol
                        select simb.ID;

                if (s.Count() == 0)
                {
                    Console.WriteLine("NO SE ENCONTRO EL SIMBOLO.");
                    continue;
                }
                return s.First();
            }
        }




        public static void CreateSymbol()
        {
            Console.WriteLine("CREATE SYMBOL");
            Console.Write("Simbolo: ");
            var symbol = (Console.ReadLine() + "").Trim().ToUpper();
            Console.Write("Link Cotizaciones: ");
            var link = Console.ReadLine();


            var db = Bolseye.Support.GetDB();

            var s = new Bolseye.Simbolo();
            s.Simb = symbol;
            s.LinkCotizaciones = link;
            s.UltimaActualizacion = DateTime.UtcNow;


            db.AddToSimbolos(s);


            int aff = db.SaveChanges();

            Console.WriteLine("SYMBOL ADDED SUCCESSFULLY ! Press a key to end.");
            Console.ReadKey();
        }




        public static float GetCurrency(string header)
        {
            Console.Write(header);
            while (true)
            {
                var currency = Console.ReadLine();
                float value;
                if (float.TryParse(currency, out value))
                {
                    Console.WriteLine(value.ToString("$0.00"));
                    return value;
                }
                Console.WriteLine("Invalid number");
            }
        }
    }
}